<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>地点关键字 + 驾车路线规划</title>
    <style type="text/css">
        html,
        body,
        #container {
            width: 100%;
            height: 100%;
        }
    </style>
    <style type="text/css">
        #panel {
            position: fixed;
            background-color: white;
            max-height: 90%;
            overflow-y: auto;
            top: 10px;
            right: 10px;
            width: 280px;
        }

        #panel .amap-call {
            background-color: #009cf9;
            border-top-left-radius: 4px;
            border-top-right-radius: 4px;
        }

        #panel .amap-lib-driving {
            border-bottom-left-radius: 4px;
            border-bottom-right-radius: 4px;
            overflow: hidden;
        }

        
    </style>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
    <script type="text/javascript">
        window._AMapSecurityConfig = {
            securityJsCode: "5f4534cc9687a1f9ad1aee97d0b79343",
        };
    </script>
    <script type="text/javascript"
        src="https://webapi.amap.com/maps?v=1.4.15&key=a056ef6e8bf5bb890784322f380b3cfa&plugin=AMap.Driving"></script>
    <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>

</head>

<body>
    起始地：<input type="text" style="width: 100px;"><br>
    目的地：<input type="text" style="width: 100px;">
    <button onclick="fn()">开始规划</button>
    <div id="container"></div>
    <div id="panel"></div>
    <div id="container"></div>
    <div class="info">
        <h4>预报天气</h4>
        <hr>
        <p id='forecast'></p>
    </div>
    <!-- 高德地图 -->
    <script type="text/javascript">
        var inp1 = document.querySelectorAll("input")[0];
        var inp2 = document.querySelectorAll("input")[1];
        //基本地图加载
        var map = new AMap.Map("container", {
            resizeEnable: true,
            center: [113.38, 34.79], // 更改为北京的默认坐标
            zoom: 13 //地图显示的缩放级别

        });


        AMap.plugin('AMap.Weather', function () {
            var weather = new AMap.Weather();

            weather.getLive('荥阳市', function (err, data) {
                if (!err) {
                    var str = [];
                    str.push('<h4 >实时天气' + '</h4><hr>');
                    str.push('<p>城市/区：' + data.city + '</p>');
                    str.push('<p>天气：' + data.weather + '</p>');
                    str.push('<p>温度：' + data.temperature + '℃</p>');
                    str.push('<p>风向：' + data.windDirection + '</p>');
                    str.push('<p>风力：' + data.windPower + ' 级</p>');
                    str.push('<p>空气湿度：' + data.humidity + '</p>');
                    str.push('<p>发布时间：' + data.reportTime + '</p>');
                    var marker = new AMap.Marker({ map: map, position: map.getCenter() });
                    var infoWin = new AMap.InfoWindow({
                        content: '<div class="info" style="position:inherit;margin-bottom:0;">' + str.join('') + '</div><div class="sharp"></div>',
                        isCustom: true,
                        offset: new AMap.Pixel(0, -37)
                    });
                    infoWin.open(map, marker.getPosition());
                    marker.on('mouseover', function () {
                        infoWin.open(map, marker.getPosition());
                    });
                }
            });
            //未来4天天气预报
            weather.getForecast('朝阳区', function (err, data) {
                if (err) { return; }
                var str = [];
                for (var i = 0, dayWeather; i < data.forecasts.length; i++) {
                    dayWeather = data.forecasts[i];
                    str.push(dayWeather.date + ' <span class="weather">' + dayWeather.dayWeather + '</span> ' + dayWeather.nightTemp + '~' + dayWeather.dayTemp + '℃');
                }
                document.getElementById('forecast').innerHTML = str.join('<br>');
            });
        });

        // 添加中心坐标标记
        const marker = new AMap.Marker({
            position: [113.38, 34.79], //地图标记点位置
        });

        map.add(marker); //添加到地图
        function fn() {
            //构造路线导航类
            var driving = new AMap.Driving({
                map: map,
                panel: "panel"
            });
            // 根据起终点名称规划驾车导航路线
            driving.search([
                { keyword: inp1.value, city: '北京' },
                { keyword: inp2.value, city: '北京' }
            ], function (status, result) {
                if (status === 'complete') {
                    log.success('绘制驾车路线完成')
                } else {
                    log.error('获取驾车数据失败：' + result)
                }
            });
        }

        document.querySelector("button").addEventListener("click", fn);
    </script>
 
</body>

</html>